package com.customization.unionpay.contract.dao;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ContractLedgerDao {

    private static final Log logger = LogFactory.getLog(ContractLedgerDao.class);
    // 合同收付款关系: 收款合同
    private static final int HTSFKGX_SKHT = 1;
    // 合同收付款关系: 支出合同
    private static final int HTSFKGX_ZCHT = 2;
    private static final String HTSKJHTZ = "uf_htskjhtz"; //合同收款计划台账
    private static final String HTSKJHTZZX = "uf_htskjhtz"; //合同收款计划台账ZX
    private static final String HTFKJHTZ = "uf_htjhfktz"; //合同付款计划台账
    private static final String HTFKJHTZZX = "uf_htskjhtz"; //合同付款计划台账ZX



    /**
     * 收款合同台账查询
     * @param tableName
     * @param contractNumbers
     * @param page
     * @param pageSize
     * @return
     */
    public JSONObject selectSKContractInfoByCntractNumber(String tableName,List<String> contractNumbers, Integer page, Integer pageSize) {

        List<String> paramList = Lists.newArrayList("htmc","htbh","htglbh","htcbjgmc","htcbbmmc","htcbryxm","htgljgmc","htglbmmc","htglr","htgdr","htqdsj","htdqr","htskysbde","yyyhtfj","zhtbh","glzht","htlsbz","htsfkgx","htywlx","htzx","htsjly","htzt","htzl","htbasj","htglxx","htybzje","hthl","htbhsje","hsdw","htbalc","srbzje","ysbzje","ysjzje","skje","bzzje","wbzje","htqdbz","szpphtxx","gljsrstht","srkjxy","hsbm");

        return selectContractInfoByCntractNumber(tableName,HTSFKGX_SKHT,paramList,contractNumbers,page,pageSize);
    }

    public JSONObject selectZCContractInfoByCntractNumber(String tableName,List<String> contractNumbers, Integer page, Integer pageSize) {

        List<String> paramList = Lists.newArrayList("zt","sfjc","yfje","yxdyqyfmc","gxhtzt","wfkje","fkzje","bzzje","wbzje","htmc","htbh","htglbh","htjbjg","htjbbm","htglr","htgdr","htqdsj","htdqr","htskysbde","htfkysbde","yyyhtfj","zhtbh","glzht","sfsjdbmzhsfkjh","htlsbz","htsfkgx","htywlx","htzx","htsjly","htzt","htzl","htbasj","htglxx","szpphtxx","yhdxyglxx","htybzje","htqdbz","hthl","htrmbzje","htbhsje","fyhje","zbhje","yysje","srje","ysje","jzje","skje","tkjesr","lzje","jtye","yfkhsqsqje","tkjezc","hsdw","hsbm","srkjxy","zjgcxm","zlje","zlmj","zldz","zlksrq","zljsrq","fkzq","mzksrq","mzjsrq","sfcslc","pfhxbhfj","pfhxbh","htbalc","fkbzrq","fkbzdh","fkbzr");

        return selectContractInfoByCntractNumber(tableName,HTSFKGX_ZCHT,paramList,contractNumbers,page,pageSize);
    }

    public JSONObject selectContractInfoByCntractNumber(String tableName,int htsfkgx,List<String> paramList, List<String> contractNumbers, Integer page, Integer pageSize){

        String psql =  String.join(",", paramList);

        String sql = "";
        JSONObject data = null;
        RecordSet rs = new RecordSet();
        JSONArray jsonArray = new JSONArray();
        if (null != contractNumbers) {
            String contractNumberStr = " where htbh in (" + contractNumbers.stream().map(num -> "\"" + num + "\"").collect(Collectors.joining(","))+") ";
            sql = "select id," +psql+ " from " + tableName + contractNumberStr ;
            rs.executeSql(sql);

            data = new JSONObject();

            if (rs.next()) {
                JSONObject result = new JSONObject();
                for(String key:paramList){
                    result.put(key, rs.getString(key));
                }
                String htid = "'" +  rs.getString("id") + "'";
                result.putAll(generatorDetailResult(htsfkgx,htid));
                jsonArray.add(result);
            }
            data.put("data",jsonArray);
        }else {
            int offset = (page - 1) * pageSize;
            int count = 0;

            String countSql = "select count(1) num from "+ tableName ;
            rs.execute(countSql);
            if(rs.next()){
                count = Util.getIntValue(rs.getString("num"), -1);
            }else {
                return data;
            }

            sql = "select id," +psql+ " from " + tableName  + " limit "+offset+","+pageSize;
            rs.executeSql(sql);

            data = new JSONObject();
            while (rs.next()) {
                JSONObject result = new JSONObject();
                for(String key:paramList){
                    result.put(key, rs.getString(key));
                }
                String htid = "'" +  rs.getString("id") + "'";
                result.putAll(generatorDetailResult(htsfkgx,htid));
                jsonArray.add(result);
            }
            data.put("data",jsonArray);
            data.put("pageSize",pageSize);
            data.put("count",count);
        }
        return data;
    }

    private Map<String,Object> generatorDetailResult(int htsfkgx, String htid) {
        Map<String,Object> htDetail = new HashMap<>();
        RecordSet rs = new RecordSet();
        if(htsfkgx == HTSFKGX_SKHT){
            // 收款合同
            // 收款计划 skjh
            List<String> skjhParamList = Lists.newArrayList("htmc","htwb","htglbh","htskysbde","htzt","sfkjhbh","ftgz","lykssj","lyjssj","dzjszq","jhskrq","skhtlbsrywdl","srywlxsrwxl","jjsx","hskm","sktj","jhskje","kdksl","jhskbhsje","srbzje","ysbzje","ysjzje","skje","jhzrbm","jhzrr","jhzrjg","sfybz","bzdh","sksj","bzr","bzzje","wbzje");

            JSONArray skjhList = selectJHByHTID(rs,HTSKJHTZ,htid,skjhParamList);
            if(null != skjhList){
                htDetail.put("skjh",skjhList);
            }

            // 收款计划ZX skjhzx
            List<String> skjhzxParamList = Lists.newArrayList();
            //TODO: 初始化收款计划ZX 数据项
            JSONArray skjhzxList = selectJHByHTID(rs,HTSKJHTZZX,htid,skjhzxParamList);
            if(null != skjhzxList) {
                htDetail.put("skjhzx", skjhzxList);
            }

            //
        }else {
            // 支出合同
            // 付款计划 fkjh
            List<String> fkjhParamList = Lists.newArrayList("yfkje","sjbzfkxz","yszrzx","wbzje","fkzje","wfkje","yfhxzje","htmc","htwb","htglbh","htfkysbde","htzt","sfkjhbh","sfjsjs","fkxz","zchtdl","zcywxl","ysxm","zdgz","jjsx","yskm","cbxz","jhfkrq","fktj","jhfkje","zpsl","jhfkbhsje","fyhje","zbhje","jhzrjg","jhzrbm","jhzrr","lyksrq","lyjsrq","lzje","jtye","tkje","spzje","yfje","yfyhxje","yfwhxje","htjtje","yzyhtjtjeyf","nbczljje","kzyhtjtjeyf","bzdh","fksj","bzr");

            JSONArray fkjhList = selectJHByHTID(rs,HTFKJHTZ,htid,fkjhParamList);
            if(null != fkjhList) {
                htDetail.put("fkjh", fkjhList);
            }
            // 付款计划 fkjhzx
            List<String> fkjhzxParamList = Lists.newArrayList();
            //TODO: 初始化收款计划ZX 数据项
            JSONArray fkjhzxList = selectJHByHTID(rs,HTFKJHTZZX,htid,fkjhzxParamList);
            if(null != fkjhzxList) {
                htDetail.put("fkjh", fkjhzxList);
            }
        }
        return htDetail;
    }

    private JSONArray selectJHByHTID(RecordSet rs,String tableName,String htid, List<String> paramList) {
        JSONArray jsonArray = null;
        if( paramList!=null || paramList.size()>0) {

            String psql = String.join(",", paramList);
            String sql = "select " + psql + " from " + tableName + " where htglbh = " + htid;
            rs.executeSql(sql);

            while (rs.next()) {
                JSONObject result = new JSONObject();
                for (String key : paramList) {
                    result.put(key, rs.getString(key));
                }
                if(null == jsonArray){
                    jsonArray = new JSONArray();
                }
                jsonArray.add(result);
            }
        }
        return jsonArray;
    }
}
